package com.mftour.moudles.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mftour.common.Constant;
import com.mftour.common.utils.JsonUtil;
import com.mftour.moudles.entity.ConsolePermission;
import com.mftour.moudles.entity.RolePermission;
import com.mftour.moudles.service.ConsolePermissionService;
import com.mftour.moudles.service.RolePermissionService;
import com.mftour.moudles.vo.AjaxResponse;

@Controller
@RequestMapping(value = "/permission")
public class PermissonController extends BaseController {

	@Autowired
	private ConsolePermissionService consolePermissionSer;
	@Autowired
	private RolePermissionService rolePermissionSer;

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String permissionList() {
		return "system/permissionList";
	}

	@RequestMapping(value = "/ajax/save", method = RequestMethod.POST)
	@ResponseBody
	public String save(ConsolePermission cp) {
		cp.setPermissionId(null);
		consolePermissionSer.insertSelective(cp);
		if (cp.getPermissionId() == null) {
			AjaxResponse ajaxResponse = new AjaxResponse(Constant.RS_CODE_ERROR, "添加权限出错！");
			return JsonUtil.writeValueAsString(ajaxResponse);
		}
		consolePermissionSer.refreshAllPermissions();
		AjaxResponse ajaxResponse = new AjaxResponse(Constant.RS_CODE_SUCCESS, "添加权限成功！");
		return JsonUtil.writeValueAsString(ajaxResponse);
	}

	@RequestMapping(value = "/ajax/del", method = RequestMethod.POST)
	@ResponseBody
	public String del(Integer permissionId) {
		String checkResult=consolePermissionSer.checkDelAble(permissionId);
		if (checkResult.equals("")) {
			RolePermission record=new RolePermission();
			record.setPermissionId(permissionId);
			rolePermissionSer.deleteSelective(record);
			
			consolePermissionSer.deleteByPrimaryKey(permissionId);
			
			consolePermissionSer.refreshAllPermissions();
			consolePermissionSer.refreshAllUserMenuPermissions();
			AjaxResponse ajaxResponse = new AjaxResponse(Constant.RS_CODE_SUCCESS, "删除权限成功！");
			return JsonUtil.writeValueAsString(ajaxResponse);
		}
		AjaxResponse ajaxResponse = new AjaxResponse(Constant.RS_CODE_ERROR, checkResult);
		return JsonUtil.writeValueAsString(ajaxResponse);
	}

	@RequestMapping(value = "/ajax/update", method = RequestMethod.POST)
	@ResponseBody
	public String update(ConsolePermission cp) {
		consolePermissionSer.updateByPrimaryKeySelective(cp);
		consolePermissionSer.refreshAllPermissions();
		consolePermissionSer.refreshAllUserMenuPermissions();
		AjaxResponse ajaxResponse = new AjaxResponse(Constant.RS_CODE_SUCCESS, "修改权限成功！");
		return JsonUtil.writeValueAsString(ajaxResponse);
	}
	
	@RequestMapping(value = "/ajax/entity", method = RequestMethod.GET)
	@ResponseBody
	public String getEntity(Integer permissionId) {
		ConsolePermission cp = consolePermissionSer.getConsolePermissionById(permissionId);
		AjaxResponse ajaxResponse = new AjaxResponse(Constant.RS_CODE_SUCCESS, "获取权限成功！");
		if(cp!=null){
			cp.setPermissions(null);
			ajaxResponse.setData(cp);
		}else{
			ajaxResponse.setCode(Constant.RS_CODE_ERROR);
			ajaxResponse.setMsg("获取权限失败！");
		}		
		return JsonUtil.writeValueAsString(ajaxResponse);
	}
	

}
